package com.chenbei.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chenbei.system.domain.SysOperationLog;
import com.chenbei.system.mapper.SysOperationLogMapper;
import com.chenbei.system.service.ISysOperationLogService;
import com.chenbei.system.vo.req.SysOperationLogReq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class SysOperationLogService extends ServiceImpl<SysOperationLogMapper, SysOperationLog> implements ISysOperationLogService {

    @Override
    public Page<SysOperationLog> pageList(SysOperationLogReq sysOperationLogReq) {
        LambdaQueryWrapper<SysOperationLog> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(SysOperationLog::getId);
        if (StrUtil.isNotBlank(sysOperationLogReq.getUsername())) {
            lambdaQueryWrapper.like(SysOperationLog::getUsername, sysOperationLogReq.getUsername());
        }
        if (StrUtil.isNotBlank(sysOperationLogReq.getStartDate()) && StrUtil.isNotBlank(sysOperationLogReq.getEndDate())) {
            String start = sysOperationLogReq.getStartDate() + " 00:00:00";
            String end = sysOperationLogReq.getEndDate() + " 23:59:59";
            lambdaQueryWrapper.between(SysOperationLog::getCreateTime, start, end);
        }
        Page<SysOperationLog> pageConfig = new Page<>(sysOperationLogReq.getPageNum(), sysOperationLogReq.getPageSize());
        return page(pageConfig, lambdaQueryWrapper);
    }
}
